#include <fstream>
#include <cstring>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const ll N = 2005;

ifstream fin("color.in");
ofstream fout("color.out");

ll dp[N][N];

ll arr[N];

void solve() {
    ll n;
    fin >> n;
    memset(dp,0,sizeof dp);
    for (ll i = 1; i <= n; i++) {
        fin >> arr[i];
    }
    for (ll i = 1; i <= n; i++) {
        for (ll j = 0; j < i; j++) {
            dp[i][i - 1] =
                max(dp[i][i - 1],
                    dp[i - 1][j] + (arr[i] == arr[j] ? 1 : 0) * arr[i]);
            dp[i][j] =
                max(dp[i][j],
                    dp[i - 1][j] + (arr[i] == arr[i - 1] ? 1 : 0) * arr[i]);
        }
    }
    ll ans = 0;
    for (ll i = 0; i <= n; i++) {
        ans=max(ans,dp[n][i]);
    }
    fout<<ans<<"\n";
    return;
}

int main() {
    ll t;
    fin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

/*

*/